package my.game.scenetest;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.Entity;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.AlphaModifier;
import org.andengine.entity.modifier.DelayModifier;
import org.andengine.entity.modifier.IEntityModifier;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.MoveYModifier;
import org.andengine.entity.modifier.ParallelEntityModifier;
import org.andengine.entity.modifier.ScaleAtModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.primitive.Line;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.shape.IShape;
import org.andengine.entity.util.FPSLogger;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.shader.ShaderProgram;
import org.andengine.opengl.vbo.IVertexBufferObject;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.color.Color;
import org.andengine.util.modifier.IModifier.DeepCopyNotSupportedException;
import org.andengine.util.modifier.IModifier.IModifierListener;
import org.andengine.util.modifier.LoopModifier;
import org.andengine.util.modifier.ease.EaseExponentialIn;
import org.andengine.util.modifier.ease.EaseQuartIn;

import android.view.KeyEvent;

public class SceneTest extends SimpleBaseGameActivity {
	private static final int CAMERA_WIDTH = 640;
	private static final int CAMERA_HEIGHT = 480;
	private VertexBufferObjectManager mVertexBufferObjectManager;
	private static final int 
		LAYER_COUNT = 2,
		ROAD_LAYER = 0 ,
		CAR_LAYER = ROAD_LAYER+1;
	private Scene mScene;
	private Camera mCamera;
	private Road mRoad;
	private Car mCar;
	private int shiftMutipler=1;
	private float velocity = 0f;
	@Override
	public EngineOptions onCreateEngineOptions() {
		mCamera=new Camera(0,0,(float)CAMERA_WIDTH,(float)CAMERA_HEIGHT); 
		return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT), mCamera);
	}
	
	@Override
	protected void onCreateResources() {
		// TODO Auto-generated method stub

	}

	@Override
	protected Scene onCreateScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());
		mScene = new Scene();
		mScene.setBackground(new Background(Color.BLACK));
		mVertexBufferObjectManager  = this.getVertexBufferObjectManager();
		for(int i=0;i<LAYER_COUNT;i++){
			mScene.attachChild(new Entity());
		}
		mRoad=new Road();
		mCar = new Car();
		mScene.getChildByIndex(ROAD_LAYER).attachChild(mRoad);
		mScene.getChildByIndex(CAR_LAYER).attachChild(mCar);
		/*
		final LoopEntityModifier entityModfier= new LoopEntityModifier(new ParallelEntityModifier(
				new ScaleAtModifier(10f, 0.3f, 1f, CAMERA_WIDTH/2, 0 , EaseQuartIn.getInstance()),
				new AlphaModifier(10f, 0f, 1f,EaseExponentialIn.getInstance()),
				new MoveYModifier(10f, CAMERA_HEIGHT/2, CAMERA_HEIGHT,EaseExponentialIn.getInstance())
				));
		for(int i=0;i<10;i++){
			final Line line =new Line(0,0,CAMERA_WIDTH,0,vertexBufferObjectManager);
			final SequenceEntityModifier mEntityModifier= new SequenceEntityModifier(new DelayModifier(i),entityModfier.deepCopy());
			line.registerEntityModifier(mEntityModifier);
			line.setColor(Color.WHITE);
			line.setLineWidth(1);
			scene.attachChild(line);
		}*/
		mScene.registerUpdateHandler(new IUpdateHandler() {
			
			@Override
			public void reset() {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onUpdate(float pSecondsElapsed) {
				mCar.setCrashState(mRoad.collidesWith(mCar));
			}
		});
		return mScene;
	}
	
	@Override
	public
	boolean onKeyDown(int keyCode , KeyEvent event){
		switch(keyCode){
			case KeyEvent.KEYCODE_DPAD_LEFT:
				mCar.moveX(-1*(shiftMutipler++));
				return true;
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				mCar.moveX(1*(shiftMutipler++));
				return true;
			default:
				return super.onKeyDown(keyCode, event);
		}
	}
	@Override
	public
	boolean onKeyUp(int keyCode,KeyEvent event){
		shiftMutipler=1;
		return true;
	}
	class Road extends Entity{
		private Line leftLine,rightLine,centerLine;
		private LoopEntityModifier mLoopEntityModifier=null;
		public Road(){
			super();
			/*
			leftLine=new Line(0,CAMERA_HEIGHT,CAMERA_WIDTH*0.25f,0,mVertexBufferObjectManager);
			rightLine=new Line(CAMERA_WIDTH,CAMERA_HEIGHT,CAMERA_WIDTH*0.75f,0,mVertexBufferObjectManager);
			*/
			leftLine = new Line(CAMERA_WIDTH*0.25f,CAMERA_HEIGHT,CAMERA_WIDTH*0.25f,0,mVertexBufferObjectManager);
			rightLine = new Line(CAMERA_WIDTH*0.75f,CAMERA_HEIGHT,CAMERA_WIDTH*0.75f,0,mVertexBufferObjectManager);
			centerLine = new Line(CAMERA_WIDTH*0.5f,0,CAMERA_WIDTH*0.5f,CAMERA_HEIGHT*0.3f,10f,mVertexBufferObjectManager);
			mLoopEntityModifier = new LoopEntityModifier(new ParallelEntityModifier(
					new MoveYModifier(2f,-CAMERA_HEIGHT,CAMERA_HEIGHT,EaseExponentialIn.getInstance()),
					new AlphaModifier(2f, 0.5f, 1f,EaseExponentialIn.getInstance())
					));
			centerLine.registerEntityModifier(mLoopEntityModifier);
			this.attachChild(leftLine);
			this.attachChild(rightLine);
			this.attachChild(centerLine);
		}
		public boolean collidesWith(IShape pIShape){
			return leftLine.collidesWith(pIShape)||rightLine.collidesWith(pIShape);
		}
	}
	class Car extends Rectangle{
		private boolean isCrash=false;
		final private static float
			CAR_WIDTH = CAMERA_WIDTH/5,
			CAR_HEIGHT= CAMERA_HEIGHT/10,
			UNIT_X= CAR_WIDTH*0.005f;
		Car(){
			super(CAMERA_WIDTH/2-CAR_WIDTH/2,CAMERA_HEIGHT-CAR_HEIGHT,CAR_WIDTH,CAR_HEIGHT,mVertexBufferObjectManager);
			this.setColor(Color.GREEN);
		}
		public
		void moveX(int deltaX){
			int sign=deltaX>0?1:-1,tot=deltaX/sign;
			if(isCrash)tot=1;
			for(int i=0;i<tot;i++){
				this.setX(this.getX()+sign*UNIT_X);
				if(isCrash)break;
			}
		}
		void setCrashState(boolean pCrashState){
			isCrash=pCrashState;
			if(isCrash){
				this.setColor(Color.RED);
			}
			else{
				this.setColor(Color.GREEN);
			}
		}
		boolean getCrashState(){
			return isCrash;
		}
	}
}
